package com.zb.service.impl;

import com.zb.dao.TGoodsMapper;
import com.zb.dao.TShopCartMapper;
import com.zb.model.TGoods;
import com.zb.model.TGoodsInShopCart;
import com.zb.service.ShopCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Service
public class ShopCartServiceImpl implements ShopCartService{

	@Autowired
    TShopCartMapper sc;
	
	@Autowired
    TGoodsMapper tgm;
	
	public List<TGoodsInShopCart> addGoodsIntoShopCartService(String goodsId,String userId,int count) {
		List<TGoodsInShopCart> TGoodsInScList=sc.selectAllGoodsInSC(userId);
		TGoods good=tgm.selectGoodsMsgById(goodsId);
		TGoodsInShopCart goodsInSc= new TGoodsInShopCart();
		goodsInSc.setGoodsId(good.getGoodsId());
		goodsInSc.setGoodsImg(good.getGoodsImg());
		goodsInSc.setGoodsName(good.getGoodsName());
		goodsInSc.setPrice(good.getPrice());
		goodsInSc.setUserId(userId);
		goodsInSc.setCount(count);
		System.out.println("--------加入购物车的商品信息为"+goodsInSc);
		Boolean isExist=false;
		if(TGoodsInScList.isEmpty()) {
			sc.insertIntoSc(goodsInSc);
		}
		else {
			for (TGoodsInShopCart tGoodsInShopCart : TGoodsInScList) {
				if(goodsId.equals(tGoodsInShopCart.getGoodsId())) {
					tGoodsInShopCart.setCount(tGoodsInShopCart.getCount()+count);
					sc.updateGoodsCount(tGoodsInShopCart);
					TGoodsInScList=sc.selectAllGoodsInSC(userId);
					isExist=true;
					break;
				}
			}
			if(!isExist) {
				sc.insertIntoSc(goodsInSc);
			}
		}
		return TGoodsInScList;
	}
	
	public TGoodsInShopCart updateCount(String goodsId,String userId,String method) {
		TGoodsInShopCart tGoodsInShopCart= new TGoodsInShopCart();
		tGoodsInShopCart.setGoodsId(goodsId);
		tGoodsInShopCart.setUserId(userId);
		TGoodsInShopCart good=sc.selectSingleGoods(tGoodsInShopCart);
		System.out.println("商品信息为："+good);
		if (method.equals("+")) {
			good.setCount(good.getCount()+1);
			sc.updateGoodsCount(good);
		} else {
			good.setCount(good.getCount()-1);
			sc.updateGoodsCount(good);
		}
		return good;
		
	}
	
	public List<TGoodsInShopCart> selectAllGoodsInSc(String userId) {
		return sc.selectAllGoodsInSC(userId);
	}

	public List<TGoodsInShopCart> delGoodsIntoShopCartService(String goodsId, String userId) {
		System.out.println("������delGoodsIntoShopCartService");
		TGoodsInShopCart tGoodsInShopCart= new TGoodsInShopCart();
		tGoodsInShopCart.setGoodsId(goodsId);
		tGoodsInShopCart.setUserId(userId);
		System.out.println(goodsId+"--"+userId);
		sc.delGoodsInSc(tGoodsInShopCart);
		//sc.delGoodsInSc(goodsId,userId);
		return sc.selectAllGoodsInSC(userId);
	}
	
	public float allSelectSum(String userId,HttpSession session) {
		List<TGoodsInShopCart> goodsInScList=sc.selectAllGoodsInSC(userId);
		session.setAttribute("goodsInScList", goodsInScList);
		float sum=0;
		for (TGoodsInShopCart tGoodsInShopCart : goodsInScList) {
			sum+=tGoodsInShopCart.getCount()*tGoodsInShopCart.getPrice();
		}
		return sum;
	}

	public float SingleSelectChangeSum(String goodsId, String userId, float sum, String method,HttpSession session) {
		TGoodsInShopCart tGoodsInShopCart= new TGoodsInShopCart();
		tGoodsInShopCart.setGoodsId(goodsId);
		tGoodsInShopCart.setUserId(userId);
		TGoodsInShopCart good=sc.selectSingleGoods(tGoodsInShopCart);
		if (method.equals("+")) {
			sum+=good.getCount()*good.getPrice();
			if(session.getAttribute("goodsInScList")==null) {
				List<TGoodsInShopCart> goodsInScList=new ArrayList<TGoodsInShopCart>();
				goodsInScList.add(good);
				session.setAttribute("goodsInScList", goodsInScList);
			}else {
				List<TGoodsInShopCart> goodsInScList=(List<TGoodsInShopCart>) session.getAttribute("goodsInScList");
				goodsInScList.add(good);
				session.setAttribute("goodsInScList", goodsInScList);
			}
		} else {
			sum-=good.getCount()*good.getPrice();
			List<TGoodsInShopCart> goodsInScList=(List<TGoodsInShopCart>) session.getAttribute("goodsInScList");
			goodsInScList.remove(good);
			session.setAttribute("goodsInScList",goodsInScList);
		}
		return sum;
	}

	public List<TGoodsInShopCart> afterPayToShopCart(String userId,List<TGoodsInShopCart> goodsInScList) {
		for (TGoodsInShopCart tGoodsInShopCart : goodsInScList) {
			//sc.delGoodsInSc(tGoodsInShopCart);
		}
		return sc.selectAllGoodsInSC(userId);
	}
}
